<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <link rel="shortcut icon" href="static/img/favicon32.ico" />

    <script src="webjars/webstomp-client/dist/webstomp.js"></script>

    <script src="static/js/sockjs.js"></script>
    <script src="static/js/stomp.js"></script>
</head>
<body>
    <p>首页</p>

    <div id="container"></div>

    <script>
        let url = 'ws://' + window.location.host + '/stomp';

        let payload = JSON.stringify({'id': 1, 'message': 'client message', 'time': new Date()});
    </script>

    <script>
        let socket = new WebSocket(url);

        let stomp = Stomp.over(socket);

        stomp.connect({}, function (frame) {
            stomp.send("/app/spittle/trade", {}, payload);

            stomp.subscribe("/app/spittle/get", function (message) {
                let spittles = JSON.parse(message.body);
                spittles.forEach(function (spittle) {
                    console.log(spittle);
                })
            });

        }, function (error) {
            console.log(error);
        });
    </script>

    <script>
        // 使用 webstomp

        // let socket = new WebSocket(url);
        //
        // let stomp = webstomp.over(socket)
        //
        // stomp.connect({}, function (frame) {
        //     // 目的地、头信息、数据
        //     console.log('send message');
        //     stomp.send('/app/marco', payload, {});
        // }, function (error) {
        //     console.log(error);
        // });

    </script>

<!--    <script>-->
<!--        let url = 'ws://' + window.location.host + '/marco';-->

<!--        let socket = new WebSocket(url);-->

<!--        socket.onopen = function () {-->
<!--            console.log('Opening')-->
<!--            sayMarco();-->
<!--        }-->

<!--        socket.onmessage = function (ev) {-->
<!--            console.log('Received message: ', ev.data);-->
<!--            setTimeout(function () {-->
<!--                sayMarco();-->
<!--            }, 2000);-->
<!--        }-->

<!--        function sayMarco() {-->
<!--            console.log('Sending Marco!');-->
<!--            socket.send('Marco!');-->
<!--        }-->
<!--    </script>-->
</body>
</html>